   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.event.*;
 
/* MultipleGui.java requires no other files. */
   public class MultipleGui extends JPanel
                      implements ListSelectionListener {
      private JList list;
      private DefaultListModel listModel;
   
   
      private static final String fireString = "Delete";
      private static final String createString = "Create Files";
      private JButton fireButton;
      private JButton createButton;
   
   
      public MultipleGui() {
         super(new BorderLayout());
      
         listModel = new DefaultListModel();
         listModel.addElement("TestEvent");
         listModel.addElement("DateEvent");
      
      
        //Create the list and put it in a scroll pane.
         list = new JList(listModel);
         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         list.setSelectedIndex(0);
         list.addListSelectionListener(this);
         list.setVisibleRowCount(5);
         JScrollPane listScrollPane = new JScrollPane(list);
      
      
         fireButton = new JButton(fireString);
         fireButton.setActionCommand(fireString);
         fireButton.addActionListener(new FireListener());
        
         createButton = new JButton(createString);
         createButton.setActionCommand(createString);
         createButton.addActionListener(new CreateListener());
      
        
       
         String name = listModel.getElementAt(
                              list.getSelectedIndex()).toString();
      
        //Create a panel that uses BoxLayout.
         JPanel buttonPane = new JPanel();
         buttonPane.setLayout(new BoxLayout(buttonPane,
                                           BoxLayout.LINE_AXIS));
         buttonPane.add(fireButton);
         buttonPane.add(Box.createHorizontalStrut(5));
         buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
         buttonPane.add(Box.createHorizontalStrut(5));
         buttonPane.add(createButton);
         buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
      
         add(listScrollPane, BorderLayout.CENTER);
         add(buttonPane, BorderLayout.PAGE_END);
      }
   
      class FireListener implements ActionListener {
         public void actionPerformed(ActionEvent e) {
            //This method can be called only if
            //there's a valid selection
            //so go ahead and remove whatever's selected.
            int index = list.getSelectedIndex();
            listModel.remove(index);
         
            int size = listModel.getSize();
         
            if (size == 0) { //Nobody's left, disable firing.
               fireButton.setEnabled(false);
            
            } 
            else { //Select an index.
               if (index == listModel.getSize()) {
                    //removed item in last position
                  index--;
               }
            
               list.setSelectedIndex(index);
               list.ensureIndexIsVisible(index);
            }
         }
      }
    
      class CreateListener implements ActionListener {
         public void actionPerformed(ActionEvent e) {
            //This method can be called only if
            //there's a valid selection
            //so go ahead and remove whatever's selected.
            int index = list.getSelectedIndex();
            listModel.remove(index);
         
            int size = listModel.getSize();
         
            if (size == 0) { //Nobody's left, disable firing.
               fireButton.setEnabled(false);
            
            } 
            else { //Select an index.
               if (index == listModel.getSize()) {
                    //removed item in last position
                  index--;
               }
            
               list.setSelectedIndex(index);
               list.ensureIndexIsVisible(index);
            }
         }
      }
   
   
    //This method is required by ListSelectionListener.
      public void valueChanged(ListSelectionEvent e) {
         if (e.getValueIsAdjusting() == false) {
         
            if (list.getSelectedIndex() == -1) {
            //No selection, disable fire button.
               fireButton.setEnabled(false);
            
            } 
            else {
            //Selection, enable the fire button.
               fireButton.setEnabled(true);
            }
         }
      }
   
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
      private static void createAndShowGUI() {
        //Create and set up the window.
         JFrame frame = new JFrame("MultipleGui");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        //Create and set up the content pane.
         JComponent newContentPane = new MultipleGui();
         newContentPane.setOpaque(true); //content panes must be opaque
         frame.setContentPane(newContentPane);
      
        //Display the window.
         frame.pack();
         frame.setVisible(true);
      }
      public static void main2(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
         javax.swing.SwingUtilities.invokeLater(
               new Runnable() {
                  public void run() {
                     createAndShowGUI();
                  }
               });
      }
   }
